package com.gerfmarquez.nymph.ui.seekbar;

import android.content.Context;
import android.graphics.Canvas;
import android.util.AttributeSet;
import android.view.GestureDetector;
import android.view.MotionEvent;
import android.widget.SeekBar;

import com.gerfmarquez.nymph.ui.gesture.detectors.YScrollDetector;

public class VerticalSeekBar extends SeekBar  {
	
	private static String TAG = "VerticalSeekBar";
	GestureDetector gestureDetectorYScroll;
	private int musicNote = 0;


	public VerticalSeekBar(Context context) {
		super(context);
		gestureDetectorYScroll = new GestureDetector(new YScrollDetector());
    }

    public VerticalSeekBar(Context context, AttributeSet attrs) {
        super(context, attrs);
        
        gestureDetectorYScroll = new GestureDetector(new YScrollDetector());
    }

    protected void onSizeChanged(int w, int h, int oldw, int oldh) {
        super.onSizeChanged(h, w, oldh, oldw);
    }
    public void onSizeChanged(int width, int height) {
    	this.onSizeChanged(width, height, 1, 1);
    }

    @Override
    protected synchronized void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(heightMeasureSpec, widthMeasureSpec);
        setMeasuredDimension(getMeasuredHeight(), getMeasuredWidth());
    }

    protected void onDraw(Canvas c) {
        c.rotate(-90);
        c.translate(-getHeight(), 0);

        super.onDraw(c);
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
    	int action = event.getAction();
    	int progress = getMax() -(int) (getMax() * event.getY() / getHeight());
        if (!isEnabled()) {
            return false;
        }
		if(gestureDetectorYScroll.onTouchEvent(event)) {
	        switch (action) {
		        case MotionEvent.ACTION_DOWN://not catched since ontouchevent just returns true if action move is set
		        case MotionEvent.ACTION_UP://not catched since ontouchevent just returns true if action move is set
	            case MotionEvent.ACTION_MOVE:
//	            	Log.d(TAG, "ACTION_MOVE:::::::::::::::::::::::::"+action);
	            	setProgress(progress);
	            	onSizeChanged(getWidth(), getHeight(), 0, 0);
	            	setOnlyAcceptedProgress(progress, event);
	                break;
	            case MotionEvent.ACTION_CANCEL://not catched since ontouchevent just returns true if action move is set
//	            	Log.d(TAG, "ACTION_CANCEL:::::::::::::::::::::::::"+action);
	        }
		}
//		if(action != MotionEvent.ACTION_DOWN ) {// Not when action is down
//		}
        return true;
    }

    private boolean isMultiple(int numerator, int denominator) {
    	int module =  numerator%denominator;
    	if( module == 0) {
//    		Log.d(TAG, "Module = " +module+" numerator = " +numerator+" denominator = " +denominator);
    		return true;
    	} else {
    		return false;
    	}
    }
    
    @Override
	protected void drawableStateChanged() {
		// TODO Auto-generated method stub
		super.drawableStateChanged();
		
	}

	private void setOnlyAcceptedProgress( int progress, MotionEvent event) {
        int musicNotesLevels = getMax()/12;
        
        int noteNumber = ((progress/musicNotesLevels));

        	if((!(isMultiple(progress,musicNotesLevels))) 
        			&& !(progress < 0)
        			&& !(progress > 106)) {
        		if(noteNumber > -1 && noteNumber < 13 ) {
//        			Log.d(TAG,"Original Progress = " + progress);
        			int countIncrement = 0;
        			int countDecrement = 0;
        			int copyProgressIncrement = progress;
        			int copyProgressDecrement = progress;
        			while(!isMultiple(copyProgressIncrement, musicNotesLevels)) {
        				countIncrement++;
        				copyProgressIncrement++;
        			}
        			while(!isMultiple(copyProgressDecrement, musicNotesLevels)) {
        				countDecrement++;
        				copyProgressDecrement--;
        			}
//        			Log.d(TAG,"Increment  = "+countIncrement);
//        			Log.d(TAG,"Decrement = "+countDecrement);
        			if(countIncrement > countDecrement) {
        				progress -= countDecrement;
//        				Log.d(TAG,"Decrement TRUE "+countDecrement);
        			} else if (countDecrement >= countIncrement) {//give priority to increment against decrement when equal
        				progress += countIncrement;
//        				Log.d(TAG,"Increment TRUE "+countIncrement);
        			}
        			noteNumber = ((progress/musicNotesLevels));
        			if(noteNumber > -1 && noteNumber < 13 ) {
        				musicNote = noteNumber;
//        			Log.d(TAG,"Progress = "+progress+" musicNotesLevels = "+musicNotesLevels);
//        				Log.d(TAG,"Note number = "+noteNumber);
        				setProgress(progress);
        				onSizeChanged(getWidth(), getHeight(), 0, 0);
        			}
        		}
        	} else {
        		setProgress(progress);
        		onSizeChanged(getWidth(), getHeight(), 0, 0);
        		
        	}
    }
    public int getMusicNote() {
    	return musicNote;
    }
    
}
